package org.liuyunfeng.other;

import com.rabbitmq.client.ConnectionFactory;
import org.junit.jupiter.api.Test;
import org.liuyunfeng.RabbitProducerApplication;
import org.liuyunfeng.domain.Order;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @author: TanJingyu
 * @create: 2022-03-18 16:33
 **/
@SpringBootTest(classes = RabbitProducerApplication.class)
public class RabbitProducerTestApplication {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private RabbitAdmin rabbitAdmin;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Test
    public void test() {
        System.out.println(rabbitAdmin);
        System.out.println(rabbitAdmin);
        System.out.println(amqpTemplate);
    }

    @Test
    public void connectionTest() {
        System.out.println(connectionFactory.getConnectionCacheSize());
        Connection connection = connectionFactory.createConnection();
    }

    @Test
    public void createQueue() {
        Queue queue = new Queue("order-create");
        System.out.println(rabbitAdmin.declareQueue(queue));
    }

    @Test
    public void confirmSend() {
        rabbitTemplate.invoke(rt -> {
            rt.convertAndSend("test", Order.randomOrder());
            return "haha";
        }, (tag, mul) -> {
            System.out.println(tag);
            System.out.println(mul);
        }, (tag, mul) -> {
            System.out.println(tag);
            System.out.println(mul);
        });
    }

    @Test
    public void sendMsg() {
        for (int i = 0; i < 3; i++) {
            rabbitTemplate.convertAndSend("test", Order.randomOrder());
        }
    }

    @Test
    public void createOrder() {
        rabbitTemplate.convertAndSend("order-create-ex", "order-create", Order.randomOrder());
//        rabbitTemplate.send();
    }
}

    